<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索区和按钮 -->
      <el-row :gutter="12">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryParam.query"
                    class="input-with-select">
            <el-button slot="append" @click="getgoodList"
                       icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表区 -->
      <el-table :data="goodList"
                border
                style="width: 100%">
        <el-table-column prop="date"
                         label="#"
                         type="index">
        </el-table-column>
        <el-table-column prop="goods_name"
                         width="700px"
                         label="商品名称">
        </el-table-column>
        <el-table-column prop="goods_price"
                         label="价格">
        </el-table-column>
        <el-table-column prop="goods_number"
                         label="数量">
        </el-table-column>
        <el-table-column prop="goods_state"
                         label="商品状态">
        </el-table-column>
        <el-table-column prop="add_time"
                         label="创建时间">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary"
                       size="mini"
                       icon="el-icon-edit">编辑</el-button>
            <el-button type="danger"
                       size="mini" @click="remove(scope.row.goods_id)"
                       icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="pageInfo.pageNumber"
                     :page-sizes="[10, 20, 30, 40]"
                     :page-size="pageInfo.pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="pageInfo.totalRecord">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      goodList: [],
      pageInfo: {
        pageSize: 10,
        pageNumber: 10,
        totalRecord: 0
      },
      queryParam: {
        query: '',
        pagenum: 1,
        pagesize: 10
      }
    }
  },
  created () {
    this.getgoodList()
  },
  methods: {
    async getgoodList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryParam
      })
      if (res.meta.status !== 200) {
        this.$message.error('获取商品列表失败')
      }
      this.goodList = res.data.goods
      this.pageInfo.pageNumber = parseInt(res.data.pagenum)
      this.pageInfo.totalRecord = res.data.total
      this.pageInfo.pageSize = this.queryParam.pagesize
    },
    handleSizeChange (val) {
      this.queryParam.pagesize = val
      this.pageInfo.pageSize = val
      this.getgoodList()
    },
    handleCurrentChange () {},
    // 删除商品
    async remove (goodId) {
      const clickResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (clickResult !== 'confirm') {
        this.$message.info('已取消删除')
        return
      }
      // 发送请求删除商品
      const res = await this.$http.delete(`goods/${goodId}`)

      if (res.status !== 200) {
        return this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      this.getgoodList()
    },
    // 跳转到添加商品页面
    goAddPage () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style>
</style>
